import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    redirect: '/welcome',
    meta: { title: '首页', requiresAuth: true },
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('../views/Welcome.vue'),
        meta: { title: '欢迎', requiresAuth: true }
      },
      // 用户管理
      {
        path: '/user',
        name: 'User',
        component: () => import('../views/user/User.vue'),
        meta: { title: '系统用户管理', requiresAuth: true }
      },
      {
        path: '/role',
        name: 'Role',
        component: () => import('../views/user/Role.vue'),
        meta: { title: '角色管理', requiresAuth: true }
      },
      // 招待费用电子凭证
      {
        path: '/receipt-upload',
        name: 'ReceiptUpload',
        component: () => import('../views/receipt/ReceiptUpload.vue'),
        meta: { title: '电子凭证采集', requiresAuth: true }
      },
      {
        path: '/receipt-verify',
        name: 'ReceiptVerify',
        component: () => import('../views/receipt/ReceiptVerify.vue'),
        meta: { title: '智能票据鉴真中心', requiresAuth: true }
      },
      {
        path: '/receipt-result',
        name: 'ReceiptResult',
        component: () => import('../views/receipt/ReceiptResult.vue'),
        meta: { title: '结果输出', requiresAuth: true }
      },
      {
        path: '/receipt-history',
        name: 'ReceiptHistory',
        component: () => import('../views/receipt/ReceiptHistory.vue'),
        meta: { title: '历史结果查询', requiresAuth: true }
      },
      // 私车公用
      {
        path: '/car-verify',
        name: 'CarVerify',
        component: () => import('../views/car/CarVerify.vue'),
        meta: { title: '用车合规性验证', requiresAuth: true }
      },
      {
        path: '/car-rule',
        name: 'CarRule',
        component: () => import('../views/car/CarRule.vue'),
        meta: { title: '连续性检验规则', requiresAuth: true }
      },
      {
        path: '/car-amount',
        name: 'CarAmount',
        component: () => import('../views/car/CarAmount.vue'),
        meta: { title: '金额逻辑验证', requiresAuth: true }
      },
      {
        path: '/car-control',
        name: 'CarControl',
        component: () => import('../views/car/CarControl.vue'),
        meta: { title: '时空中央控制', requiresAuth: true }
      },
      // 数据统计
      {
        path: '/statistics',
        name: 'Statistics',
        component: () => import('../views/statistics/Statistics.vue'),
        meta: { title: '多维度统计数据', requiresAuth: true }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title} - 铁塔财务部智能化报销管控平台`
  
  if (to.meta.requiresAuth) {
    const token = store.state.user.token
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router 